package bar.dark.lee.alarm.add.date;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.SuperActivity;

/**
 * 요일 리스트 화면
 * @author HyoJun Lee
 *
 */
public class DateListActivity extends SuperActivity
{
	public static final String CHECKSTRING = "check_string";
	public static final String CHECKDATA = "check_data";
	public static final String CHECKSELECT = "check_select";
	
	private ListView listViewDate;
	private Button btnOk;
	
	private ArrayList<String> listDate;
	private ArrayList<String> listDateSelect;
	
	private int [] checkBox;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.datelist_activity);
		
		setTitle(gets(R.string.datelist_title));
		
		setFindView();
		setDataSetting();
		setClickListener();
	}
	
	@Override
	public void setFindView()
	{
		listViewDate = (ListView) findViewById(R.id.datelist_list);
		btnOk = (Button) findViewById(R.id.datelist_btnOk);
	}

	@Override
	public void setDataSetting()
	{
		Intent intent = getIntent();
		listDate = intent.getStringArrayListExtra(CHECKDATA);
		listDateSelect = intent.getStringArrayListExtra(CHECKSELECT);
		
		if (listDate == null)
		{
			return;
		}
		
		if (listDateSelect == null)
		{
			return;
		}
		
		checkBox = new int[listDate.size()];
		
		for (int i = 0; i < checkBox.length; i++)
		{
			checkBox[i] = 0;
		}
		
		if (listDateSelect.size() > 0)
		{
			for (int i=0; i<listDateSelect.size(); i++)
			{
				checkBox[Integer.parseInt(listDateSelect.get(i))] = 1;
			}
		}
		
		DateListAdapter adapter = new DateListAdapter(this, R.layout.list_item_date, listDate);
		listViewDate.setAdapter(adapter);
		listViewDate.setOnItemClickListener(adapter);
		listViewDate.setDividerHeight(1);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void setClickListener()
	{
		btnOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra(CHECKSELECT, checkBox);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}
	
	@Override
	protected void tipButton()
	{
	}
	
	private class DateListAdapter extends ArrayAdapter<String> implements OnItemClickListener
	{
		private Context context;
		private int viewId;
		private ArrayList<String> list;
		
		public DateListAdapter(Context context, int textViewResourceId, ArrayList<String> list) 
		{
			super(context, textViewResourceId, list);
			this.context = context;
			this.viewId = textViewResourceId;
			this.list = list;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v  = li.inflate(viewId, null);
			}
			
			String data = list.get(position);
			
			TextView tv = (TextView) v.findViewById(R.id.datelist_item_text);
			CheckBox cb = (CheckBox) v.findViewById(R.id.datelist_item_check);
			
			if (checkBox[position] == 1)
			{
				cb.setChecked(true);
			}
			else
			{
				cb.setChecked(false);
			}
			
			tv.setText(data + gets(R.string.datelist_day));
			
			return v;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
			
			CheckBox cb = (CheckBox) v.findViewById(R.id.datelist_item_check);
			
			if (cb.isChecked())
			{
				cb.setChecked(false);
				checkBox[position] = 0;
			}
			else
			{
				cb.setChecked(true);
				checkBox[position] = 1;
			}
		}
	}
}
